package com.horoscope.astrology.zodiac.signs.free.fragment.article

import android.os.Bundle
import android.util.Log
import android.util.SparseArray
import android.view.View
import com.horoscope.astrology.zodiac.signs.free.adapter.ArticleAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.ArticleResultBean
import com.horoscope.astrology.zodiac.signs.free.bean.ArticleResultDataBean
import com.horoscope.astrology.zodiac.signs.free.network.NetworkEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkListener
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import com.horoscope.astrology.zodiac.signs.free.utils.ToastUtil

/**
 * Article 页 具体的四个Fragment
 * */
class ArticleSubFragment : BaseSwipeRefreshFragment() {

    private var mAdapter: ArticleAdapter? = null
    private var title = ""
    private var list = ArrayList<ArticleResultDataBean>()
    private var isTwo = false

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        title = arguments?.getString("title")!!
        mAdapter = ArticleAdapter(activity)
        mAdapter!!.setTitleOfFragment(title)
        mRecyclerView.adapter = mAdapter
        SysUtils.setMaxFlingVelocity(mRecyclerView, 3000)
    }

    fun notifyAd() {
        mAdapter?.notifyDataSetChanged()
    }

    fun doubleClick() {
        val flag = mRecyclerView.canScrollVertically(-1)
        if (flag) {
            mRecyclerView.smoothScrollToPosition(0)
        } else {
            mPtrFrameLayout?.postDelayed({
                mPtrFrameLayout?.autoRefresh(true)
            }, 0)
            //上拉刷新
        }
    }

    companion object {
        val spareArray = SparseArray<String>()
        fun title(position: Int): String {
            spareArray.put(0, "Hot")
            spareArray.put(1, "Love")
            spareArray.put(2, "Career")
            spareArray.put(3, "Other")
            return spareArray[position]
        }
    }

    // 下拉加载
    override fun requestData() {
        super.requestData()
        include!!.visibility = View.GONE
        NetworkEngine.getNews(title, (currentPage++).toString(), listener)
    }

    private val listener = object : NetworkListener<ArticleResultBean>() {
        override fun onSuccess(t: ArticleResultBean) {
            if (isAdded) {
                list.clear()
                list = t.data
                include!!.visibility = View.GONE

                //adList = LoadGoogleAd.mInstance.getNativeAd("Native_List")
                // 添加了三个原生广告位
                if (t.data.size == 10) {
                    t.data.add(2, ArticleResultDataBean(1))
                    t.data.add(7, ArticleResultDataBean(1))
                    t.data.add(12, ArticleResultDataBean(1))
                }
                mAdapter?.setData(t.data)
                header?.setSuccess(true)
                mPtrFrameLayout?.refreshComplete()
            }
        }

        override fun onFailed(t: ArticleResultBean) {
            super.onFailed(t)
            errorMake()
        }

        override fun onError(e: String?) {
            super.onError(e)
            errorMake()
        }
    }

    private fun errorMake() {
        currentPage--
        header?.setSuccess(false)
        mPtrFrameLayout?.refreshComplete()
        try {
            if (isAdded && !isHidden && list.size != 0) {
                ToastUtil.showRewardError(activity)
            } else {
                include?.let {
                    include!!.visibility = View.VISIBLE
                }
            }
        } catch (e: Exception) {
            ToastUtil.showRewardError(activity)
        }
    }


    // 上拉加载
    override fun loadMore() {
        super.loadMore()
        NetworkEngine.getNews(title, (currentPage++).toString(), listenerM)

    }

    // 广告两种情况 244 或 442
    var temp = true
    private val listenerM = object : NetworkListener<ArticleResultBean>() {
        override fun onSuccess(t: ArticleResultBean) {
            if (isAdded) {
                Log.e("---", t.data.size.toString())
                if (t.data.isEmpty()) {
                    mAdapter?.setLoadFinished(true)
                }

                try { // 返回数据不稳定
                    if (t.data.size == 10) {
                        if (isTwo) { // 2 4 4
                            isTwo = false
                            t.data.add(2, ArticleResultDataBean(1))
                            t.data.add(7, ArticleResultDataBean(1))
                            t.data.add(12, ArticleResultDataBean(1))
                        } else { // 4 4
                            isTwo = true
                            t.data.add(4, ArticleResultDataBean(1))
                            t.data.add(9, ArticleResultDataBean(1))

                        }
                    }
                    mAdapter?.setMoreData(t.data)
                } catch (e: Exception) {
                    mAdapter?.setMoreData(t.data)
                }
            }
        }

        override fun onError(e: String?) {
            super.onError(e)
            loadMoreError()
        }

        override fun onFailed(t: ArticleResultBean) {
            super.onFailed(t)
            loadMoreError()
        }
    }

    private fun loadMoreError() {
        currentPage--
        if (isAdded && !isHidden) ToastUtil.showRewardError(activity)
    }
}